# -*- coding: utf-8 -*-

"""
Module implementing Dialog.
"""

import sys
import csv
import xlrd
from xlutils.copy import copy
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog,QFileDialog,QMessageBox
from PyQt5 import QtWidgets

from Ui_TransferTool import Ui_Dialog


class Dialog(QDialog, Ui_Dialog):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
        """
        Constructor
        
        @param parent reference to the parent widget
        @type QWidget
        """
        super(Dialog, self).__init__(parent)
        self.setupUi(self)

    
    @pyqtSlot()
    def on_pushButton_6_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        fileName_6V = QFileDialog.getOpenFileName(self,"select",".","execl files(*.xls *.csv)")
        self.lineEdit_6path.setText(fileName_6V[0])

    
    @pyqtSlot()
    def on_pushButton_12_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        fileName_12V = QFileDialog.getOpenFileName(self, "select", ".", "execl files(*.xls *.csv)")
        self.lineEdit_12path.setText(fileName_12V[0])


    
    @pyqtSlot()
    def on_pushButton_24_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        fileName_24V = QFileDialog.getOpenFileName(self, "select", ".", "execl files(*.xls *.csv)")
        self.lineEdit_24path.setText(fileName_24V[0])

    
    @pyqtSlot()
    def on_pushButton_Result_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        fileName_Result = QFileDialog.getOpenFileName(self, "select", ".", "execl files(*.xls *.csv)")
        self.lineEdit_result.setText(fileName_Result[0])

    
    @pyqtSlot()
    def on_pushButton_Transfer_clicked(self):
        """
        Slot documentation goes here.
        """
        # TODO: not implemented yet
        fileName_6V = self.lineEdit_6path.text()
        fileName_12V = self.lineEdit_12path.text()
        fileName_24V = self.lineEdit_24path.text()
        fileName_Result = self.lineEdit_result.text()
        try:
            if fileName_6V:
                dataList_6V = self.readData(fileName_6V)
                print(dataList_6V)
                self.writeData(fileName_Result, dataList_6V, 5)
            if fileName_12V:
                dataList_12V = self.readData(fileName_12V)
                print(dataList_12V)
                self.writeData(fileName_Result, dataList_12V, 6)
            if fileName_24V:
                dataList_24V = self.readData(fileName_24V)
                print(dataList_24V)
                self.writeData(fileName_Result, dataList_24V, 7)
            self.label_Result.setText("Pass")
            self.label_Result.setStyleSheet("color:rgb(0, 255, 0)")
        except  OSError as reason:
            QMessageBox.about(self,"文件打开出错T_T","错误的原因是" + str(reason))
            self.label_Result.setText("Fail")
            self.label_Result.setStyleSheet("color:rgb(255, 0, 0)")


    def readData(self,string):
        csvReader = csv.reader(open(string, "r", encoding="utf-8"))
        rows = [row for row in csvReader][27:]
        column = [row[14] for row in rows]
        dataList = [column[3], column[27], column[51], column[11], column[35], column[59],
                    column[7], column[15], column[31],
                    column[6], column[14], column[30],
                    column[5], column[13], column[29],
                    column[76], column[100], column[124], column[84], column[108], column[132],
                    column[80], column[104], column[128],
                    column[79], column[103], column[127],
                    column[78], column[102], column[126],
                    column[222], column[246], column[270], column[230], column[254], column[278],
                    column[226], column[250], column[274],
                    column[225], column[249], column[273],
                    column[224], column[248], column[272],
                    column[149], column[173], column[197], column[157], column[181], column[205],
                    column[153], column[177], column[201],
                    column[152], column[176], column[200],
                    column[151], column[175], column[199] ]
        dataList1=[]
        for i in dataList:
            dataList1.append(float(i))
        return dataList1


    def writeData(self,String1,dataList,column):
        xlsHandler = xlrd.open_workbook(String1, formatting_info=True)
        writeHandler = copy(xlsHandler)

        if self.checkBox_low.checkState() ^ self.checkBox_high.checkState():
            if self.checkBox_low.checkState():
                table = writeHandler.get_sheet("GSM RF Low Temperature -40℃")
                write(table, dataList,   3, column, 15)
                write(table, dataList,  64, column, 15)
                write(table, dataList, 125, column, 15)
                write(table, dataList, 186, column, 15)
                writeHandler.save(String1)

            else:
                table = writeHandler.get_sheet("GSM RF High Temperature +85℃")
                write(table, dataList, 3, column, 15)
                write(table, dataList, 64, column, 15)
                write(table, dataList, 125, column, 15)
                write(table, dataList, 186, column, 15)
                writeHandler.save(String1)
        else:
            QMessageBox.about(self, "提示：", "高低温只能且必须选择一个！")

def write(handle,readList,row,column,num):
    rows_3 = 3
    rows_64 = 64
    rows_125 = 125
    rows_186 = 186
    while num > 0:
        if row ==3:
            handle.write(rows_3, column, readList[15-num])
            rows_3 = rows_3+1
        if row ==64:
            handle.write(rows_64,column,readList[30-num])
            rows_64 = rows_64+1
        if row == 125:
            handle.write(rows_125,column,readList[45-num])
            rows_125 = rows_125 + 1
        if row == 186:
            handle.write(rows_186, column, readList[60 - num])
            rows_186 = rows_186 + 1

        num = num - 1



        
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = Dialog()
    MainWindow.show()
    sys.exit(app.exec_())
